﻿using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Car
{
    public  class CostAppService : ApplicationService, ICostAppService
    {
        private readonly IRepository<Cost, int> _OwnerRepository;
        private readonly ICostRepository _IFieIdRepository;
        public CostAppService(IRepository<Cost, int> OwnerRepository, ICostRepository IOwnerRepository)
        {

            _OwnerRepository = OwnerRepository;
            _IFieIdRepository = IOwnerRepository;
        }
        /// <summary>
        /// 货主添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public async Task<CostDto> Create(CostDto m)
        {
            var Owner = new Cost
            {
              VehicleId=m.VehicleId,
                CostPrice=m.CostPrice,
                Cmass=m.Cmass,
                Cpassenger=m.Cpassenger,
                Cway=m.Cway,
                Cresponsible=m.Cresponsible,
                Cremark=m.Cremark,
                Cdate=m.Cdate
            };

            await _IFieIdRepository.Create(Owner);
            return ObjectMapper.Map<Cost, CostDto>(Owner);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteAsync(int id)
        {
            await _IFieIdRepository.Delete(id);
        }

        public async Task<CostDto> GetCarryById(int Id)
        {
            var student = await _IFieIdRepository.GetCarryById(Id);
            return ObjectMapper.Map<Cost, CostDto>(student);
        }

        public async Task<PagedResultDto<CostDto>> GetListAsync(GetCostDto dto)
        {
            var vehiclelist = await _OwnerRepository.GetPagedListAsync(
               dto.SkipCount,
               dto.MaxResultCount,
               dto.Sorting);
            var totalCount = await AsyncExecuter.CountAsync(
               _OwnerRepository.WhereIf(
                   !dto.Filter.IsNullOrWhiteSpace(),
                   m => m.Cresponsible.Contains(dto.Filter)
               )
           );
            return new PagedResultDto<CostDto>(
             totalCount,
             ObjectMapper.Map<List<Cost>, List<CostDto>>(vehiclelist));
        }



        public async Task<int> Update(CostDto ca)
        {
            var carr = await _OwnerRepository.GetAsync(ca.Id);
            carr.VehicleId = ca.VehicleId;
            carr.CostPrice = ca.CostPrice;
            carr.Cmass = ca.Cmass;
            carr.Cpassenger = ca.Cpassenger;
            carr.Cway = ca.Cway;
            carr.Cresponsible = ca.Cresponsible;
            carr.Cremark = ca.Cremark;
            carr.Cdate = ca.Cdate;

            await _OwnerRepository.UpdateAsync(carr);
            return 1;
        }
    }
}
